package com.amado.course35Concurrent;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangyulong
 * @version 1.5.0
 * @date 2020/12/4 17:07
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
        final ThreadPoolExecutor NOTICE_MESSAGE_POOL =
                new ThreadPoolExecutor(1, 5, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10240),
                        (r) -> new Thread(r, "触发通知消息线程"),new NoticeMessageDiscardPolicy());


        NOTICE_MESSAGE_POOL.execute(()->{
            String a = null;
            //发生异常如果系统没有logger.info/error 是不会记录到的。
            System.out.println(a.toString());
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        NOTICE_MESSAGE_POOL.shutdown();
    }
    static class NoticeMessageDiscardPolicy implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("《《《因通知消息线程池处理消息速度慢，通知消息触发任务丢弃》》》");
        }
    }
}
